package applications.camoverlay;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;

public class PictureCallback implements Camera.PictureCallback {
	
	private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/";
	File dir=new File(path);

	public void onPictureTaken(byte[] data, Camera camera) {	
		String state = Environment.getExternalStorageState();

		//TODO mandare un messaggio di errore periferica SD non disponibile
		if (!Environment.MEDIA_MOUNTED.equals(state)) 
			return;
		
		//se la directory /sdcard/Pictures/ non esiste, la creo
		if(!dir.exists()){
			try{
			dir.mkdir();
			} catch(SecurityException e){
				e.printStackTrace(); //TODO messaggio di errore impossibile creare la directory Pictures sulla SD
			}
		}
		
		File picture=new File(path+System.currentTimeMillis()+".jpg");
		
		try {
			picture.createNewFile();
			FileOutputStream fos=new FileOutputStream(picture);
			fos.write(data);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace(); //TODO mandare un messaggio di errore
		}
		
		File [] files=dir.listFiles();
		
		for(int i=0;i<files.length;i++)
			Log.v("File " + i + ": ", files[i].getName());
		
		camera.startPreview();
	}

}
